package org.shj.netty.chartper7;

import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class SubReqServerHandler extends ChannelHandlerAdapter {

	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception{
		System.out.println("channelRead()");
		SubscribeReq req = (SubscribeReq) msg;
		
		if("SHJ".equalsIgnoreCase(req.getUserName())){
			System.out.println("Service accept client subscribe req: " + req.toString());
			ctx.writeAndFlush(resp(req.getSubReqId()));
		}
	}
	
	private SubscribeResp resp(int subReqId){
		SubscribeResp resp = new SubscribeResp();
		resp.setSubReqId(subReqId);
		resp.setRespCode(0);
		resp.setDesc("Netty book order succeed, 3 days later.");
		return resp;
	}
	
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception{
		System.out.println("channelReadComplete()");
		ctx.flush();
	}
	
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause){
		ctx.close();
	}
}
